package com.hpdz.hotel_back.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.hpdz.hotel_back.entity.Employee;
import com.hpdz.hotel_back.entity.Order;
import com.hpdz.hotel_back.entity.Room;
import com.hpdz.hotel_back.entity.Task;
import com.hpdz.hotel_back.service.*;
import com.hpdz.hotel_back.utils.MyPage;
import com.hpdz.hotel_back.vo.TaskVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;


/**
 * 员工: 接口！
 *
 *
 */

@RestController
public class EmployeeController {
    @Autowired
    private EmployeeService employeeService;

    @Autowired
    private RoomService roomService;

    @Autowired
    private OrderService orderService;


    @Autowired
    private TaskService taskService;

    /**
     *  接口 1: 登录
     *  name: /emp_login
     *
     *  method: post;
     *
     *  parameter: emp
     *
     *  return emp;
     */
    @PostMapping("/emp_login")
    public Employee login(@RequestBody Employee emp, HttpServletRequest request){
        Employee login = employeeService.login(emp);
        if(login == null) return null;
        HttpSession session = request.getSession(true);
        login.setPassword("");
        session.setAttribute("cur_emp",login);
        return login;
    }


    /**
     *  接口 2： 获取所有订单，分页！
     *  name: /list_order
     *
     *  method: get;
     *
     *  parameter: current,size
     *
     *  return list;
     */
     @GetMapping("/list_order/{current}/{size}")
     public IPage<Order> listOrder(@PathVariable Integer current, @PathVariable Integer size){
         return orderService.listAllOrder(current,size);
     }


    /**
     *  接口 3 : 按照订单状态,分页查找订单！
     *  name: /search_by_state
     *
     *  method: get;
     *
     *  parameter: current,size,order;
     *
     *  return list
     */
     @GetMapping("/search_by_state/{current}/{size}")
     public IPage<Order> searchByState(@PathVariable Integer current,@PathVariable Integer size,Order order){
         return orderService.findByState(current,size,order);
     }


    /**
     * 接口 4：修改订单！
     *
     *  name: /modify_order
     *
     *  method: post;
     *
     *  parameter: order;
     *
     *  return true / false;
     */
    @PostMapping("modify_order")
    public Boolean modifyOrder(@RequestBody Order order){
        return orderService.updateById(order);
    }



    /**
     *  接口 5: 添加 room
     *  name: /add_room
     *
     *  method: post;
     *
     *  parameter: room;
     *
     *  return true/false;
     */
    @PostMapping("/add_room")
    public Boolean addRoom(@RequestBody Room room){
        return roomService.save(room);
    }



    /**
     *  接口 6： 修改 room
     *  name: /modify_room
     *
     *  method: post;
     *
     *  parameter: room;
     *
     *  return true/false;
     */

    @PostMapping("/modify_room")
    public Boolean modifyRoom(@RequestBody Room room){
        return roomService.updateById(room);
    }



    /**
     *  接口 7： 获取所有room，分页！(所有state == "空闲" 的room)
     *  name: /list_room
     *
     *  method: get;
     *
     *  parameter: current,size;
     *
     *  return list;
     */
    @GetMapping("/list_room/{current}/{size}")
    public IPage<Room> listRoom(@PathVariable Integer current,@PathVariable Integer size){
        return roomService.listPage(current,size);
    }

    /**
     *  接口 8： 根据roomNumb,模糊分组查询room
     *  name: /find_room
     *
     *  method: get;
     *
     *  parameter: current,size,room;
     *
     *  return list;
     */
    @GetMapping("/find_room/{current}/{size}")
    public IPage<Room> findRoom(@PathVariable Integer current,@PathVariable Integer size,Room room){
        return roomService.findPage(current,size,room);
    }


    /**
     *  接口 9: 根据 id,查询room细节信息!
     *  name: /room_detail
     *
     *  method: get;
     *
     *  parameter: room;
     *
     *  return room;
     */
    @GetMapping("/room_detail")
    public Room roomDetail(@RequestBody Room room){
        return roomService.getById(room.getRoomId());
    }


    /**
     *  接口 9: 添加任务
     *  name: /add_task
     *
     *  method: post;
     *
     *  parameter: task;
     *
     *  return true/false;
     */
    @PostMapping("/add_task")
    public Boolean addTask(@RequestBody Task task){
        return taskService.save(task);
    }

    /**
     *  接口 10: 修改任务内容
     *  name: /modify_task
     *
     *  method: post;
     *
     *  parameter: task;
     *
     *  return true/false;
     */

    @PostMapping("/modify_task")
    public Boolean modifyTask(@RequestBody Task task){
        return taskService.updateById(task);
    }



    /**
     *  接口 11： 删除任务
     *  name: /delete_task
     *
     *  method: post;
     *
     *  parameter: task;
     *
     *  return true/false;
     */
    @PostMapping("/delete_task")
    public Boolean deleteTask(@RequestBody Task task){
        return taskService.removeById(task.getId()) ;
    }


    /**
     *  接口 12：获取任务表中所有任务(任务表原表)!
     *  name: /list_task
     *
     *  method: get;
     *
     *  parameter: current,size;
     *
     *  return IPage;
     */
    @GetMapping("/list_task/{current}/{size}")
    public IPage<Task> listTask(@PathVariable Integer current,@PathVariable Integer size){
        return taskService.listPage(current,size);
    }


    /**
     *  接口 13: 分页,查询所有人所有所有任务!
     *  name: /show_task
     *
     *  method: get;
     *
     *  parameter: current,size;
     *
     *  return myPage;
     */
    @GetMapping("/show_task/{current}/{size}")
    public MyPage<TaskVo> showTask(@PathVariable Integer current,@PathVariable Integer size){
        return taskService.showAllTask(current,size);
    }


    /**
     *  接口 14: 分页,查询某个人所有任务!
     *  name: /search_one
     *
     *  method: get;
     *
     *  parameter: current,size,emp;
     *
     *  return myPage;
     */
    @GetMapping("/search_one/{current}/{size}")
    public MyPage<TaskVo> searchTask(@PathVariable Integer current,@PathVariable Integer size,Employee emp){
        return taskService.showEmpTask(current,size,emp);
    }
}
